#include "main.h" 

string output_lang;
int main(int argc, char** argv) {
    if (argc == 1)  {
        printf("error! not input word!\n");
        return -1;
    }

    std::string need_string;
    processInputString(argc, argv, need_string, output_lang);

    CURL *curl;
    CURLcode res;
    curl = curl_easy_init();
    if (curl) {
        CTranCurl tran_curl(need_string, output_lang);

        curl_easy_setopt(curl, CURLOPT_URL, tran_curl.send_myurl.c_str());
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, processRecv);
        res = curl_easy_perform(curl);
        if(res != CURLE_OK) {
            fprintf(stderr, "curl_easy_perform() failed: %s", curl_easy_strerror(res));
        }

        curl_easy_cleanup(curl);
    }
    return 0;
}

//处理输入的字符串
int processInputString(int argc, char** argv, std::string& need_string, std::string& output_lang) {
    ostringstream ss;
    for (int i = 1; i < argc; ++i) {
        ss << argv[i] << "\n";
    }
    need_string = ss.str();

    //如果第一个字符为英文的话就设置为转化为中文
    char first_char = need_string[0];
    if ((first_char <= 'Z' && first_char >= 'A') || 
            (first_char <= 'z' && first_char >= 'a')) {
            output_lang = "zh";
    } else {
        output_lang = "en";
    }
    
    return 0;
}

size_t processRecv(void* buffer, size_t size, size_t nmemb, void* userp) {
    //读取返回的json数据并解析
    Json::Value value;
    Json::Reader reader;

    if (!reader.parse((char *)buffer, value)) {
        printf("parse json error!");
        return 0;
    }

    //取出Json中的src和dst数据
    ostringstream ss_src;  
    ostringstream ss_dst;
    Json::Value arrayObj = value["trans_result"];

    for (auto & i : arrayObj) {
        ss_src << i["src"].asString().c_str() << " ";
        ss_dst << i["dst"].asString().c_str() << " ";
    }
 
    //确定翻译类型
    std::string fromto;
    stringstream sft;
    sft << "| [ " << value["from"].asString().c_str() << "->" 
        << value["to"].asString().c_str() << " ]";
    fromto = sft.str();

    std::string final_result;
    resultSize(ss_src.str(), ss_dst.str(), final_result, fromto);

    cout << final_result;
    return size * nmemb;
}

int resultSize(const std::string& res_src, const std::string& res_dst, std::string& final_result, std::string& fromto) {
    auto size_src = res_src.size();
    auto size_dst = res_dst.size();

    //获取语言类型
    if ("zh" == output_lang) {
        size_dst = ReallZhSize(res_dst);   
    } else {
        size_src = ReallZhSize(res_src);
    }
    
    //最大长度
    auto max_size = MAX(size_src, size_dst) + DEFAULT_TYPE_LINE; 
    //cout << "max_size:[" << max_size  << "]" << endl;
    //cout << "src:[" << size_src << "] dst:[" << size_dst << "]\n";

    size_src = max_size - 10 - size_src;
    size_dst = max_size - 10 - size_dst;
    final_result = ProcessFinal(max_size, res_src, res_dst, size_src, size_dst, fromto);
    
    return 0;
}

std::string ProcessFinal(int max_size, const std::string& res_src, const std::string&res_dst, int size_src, int size_dst, std::string& fromto) {
    std::string upper;
    ProcessUpper(upper, max_size);

    stringstream sft;
    sft << fromto;
    for (int i = 0; i < max_size - 13; ++i) {
        sft << " ";
    }
    sft << "|\n";

    fromto = sft.str();
    std::string result;
    ProcessResult(result, res_src, res_dst, max_size, size_src, size_dst);
    
    stringstream ss;
    ss << upper << fromto << upper << result << upper;
   
    return ss.str();
}

int ProcessResult(std::string& result, const std::string& src, const std::string& dst, int max_size, int size_src, int size_dst) {
    stringstream ss;
    ss << "| src:[ " << src << "]";
    for (auto i = 0; i < size_src; ++i) {
        ss << " ";
    }
    ss << "|\n";
    
    ss << "| dst:[ " << dst << "]";
    for (auto i = 0; i < size_dst; ++i) {
        ss << " ";
    }
    ss << "|\n";
    
    result = ss.str();
    return 0;
}

int ProcessUpper(std::string& upper, int size) {
    stringstream ss;
    ss << "+";
    for (int i = 0; i < size-2; ++i) {
        ss << "-";
    }
    ss << "+\n";
    upper = ss.str();

    return 0;
}

bool CheckSpecialChar(const char& check_char) {
    if ((check_char < 'z' && check_char > 'a') || 
        (check_char < 'Z' && check_char > 'A')) {
        return false;
    }
    
    cout << "check\n";
    return true;
}

int ReallZhSize(const std::string& res_str) {
    int size_num = res_str.size();
    int space_num = 0;
    //去除空格影响
    for (size_t i = 0; i < res_str.size(); ++i) {
        if (res_str[i] == ' ') {
            size_num--;
            space_num++;
        }
    }

    if (!size_num) {
        return 0;
    }

    if (size_num > 1) {
        //assert((size_num) % 3 == 0);
        size_num = ((size_num) / 3) * 2;
    } else {
        size_num = 0;
    }
    //cout << "size_num:[" << size_num + space_num << "]\n";
    //把计算时的空格又加上
    return size_num + space_num;
}
